package fr.inria.arles.temperatureManagement.android;

import static fr.inria.arles.temperatureManagement.android.AndroidConstants.*;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import static fr.inria.arles.temperatureManagement.Constants.*;

/**
 * 
 * Activity to display login form and send an authentication query containing
 * the entered information to the query processor.
 * 
 * @author ajay chhatwal
 */
public class Settings extends Activity {

	EditText queryProcessorIPET;
	EditText queryProcessorPortET;
	EditText processingNodeIPET;
	EditText processingNodePortET;

	/** Handles event: Button loginB clicked */
	private final OnClickListener saveBClicked = new OnClickListener() {
		public void onClick(View v) {

			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			Editor editor = settings.edit();
			editor.putString("PROCESSING_NODE_IP", processingNodeIPET.getText()
					.toString());
			editor.putInt("PROCESSING_NODE_PORT", Integer
					.parseInt(processingNodePortET.getText().toString()));
			editor.putString("QUERY_PROCESSOR_IP", queryProcessorIPET.getText()
					.toString());
			editor.putInt("QUERY_PROCESSOR_PORT", Integer
					.parseInt(queryProcessorPortET.getText().toString()));
			editor.commit();
			finish();
		}
	};

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            the saved instance state
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		processingNodeIPET = ((EditText) findViewById(R.id.processingNodeIPET));
		processingNodePortET = ((EditText) findViewById(R.id.processingNodePortET));
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

		processingNodeIPET.setText(settings.getString("PROCESSING_NODE_IP",
				PROCESSING_NODE_IP));
		processingNodePortET.setText(settings.getInt("PROCESSING_NODE_PORT",
				PROCESSING_NODE_PORT)
				+ "");
		Button loginB = (Button) findViewById(R.id.saveB);
		loginB.setOnClickListener(saveBClicked);
	}

	/*
	 * Called when the activity is first created.
	 * 
	 * @param menu the Menu to be populated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	/*
	 * Handles item selections
	 * 
	 * @param item the selected MenuItem
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			return true;
		case R.id.exit:
			moveTaskToBack(true);
			return true;
		}
		return false;
	}

}